#!/usr/bin/env python
# -*-coding:UTF-8 -*-
'''
@Project ：19-xpath站长素材.py
@File：45-selenium_药监局.py
@Author ：文非
@Date：2021/3/1615:26
@Require：# 网页自动化程序初始
'''
import time

from selenium import webdriver
from lxml import etree
from time import sleep
import csv

# 实例化一个浏览器对象 传入浏览器的驱动程序
brows = webdriver.Chrome(executable_path="./chromedriver")
# 编写基于浏览器自动化的操作代码

url = 'https://map.tianditu.gov.cn/'
brows.get(url)
time.sleep(2)
# # 点击体验完成
brows.find_element_by_xpath('//div[@class="GuideTips_first__3ifH1"]//div//img[1]').click()
handle = brows.current_window_handle
brows.switch_to.window(handle)
# 点击图层服务列表
brows.find_element_by_xpath('//div[@class="Head_left__1Qfe9"]/span').click()
handle = brows.current_window_handle
brows.switch_to.window(handle)
time.sleep(2)
print(brows)
# 点击添加图层
brows.find_element_by_xpath('//div[@class="ThematicLayer_head__1MXVk"]/button').click()
handle = brows.current_window_handle
brows.switch_to.window(handle)
time.sleep(2)
data = brows.find_elements_by_xpath('//div[@class="ServiceResource_sourcesList__2Rwcf"]/ul//li')
print(data)

# 打开一个csv文件--设置好行列
# # csvfile_r = open("./tianditu_r.csv","r")
# csvfile_w = open("tianditu-w.txt","a",newline='')
# for da in data:
#     print(da.text)
#     with open('tianditu-w.txt', 'a', encoding='utf-8') as f:
#         f.write(da.text + "\n")
#         f.close()
#  第五页有问题 直接获取第六页 点到第六页的页面上
#  点击下三页
time.sleep(3)
brows.find_element_by_xpath("//div[@class='ServiceResource_footerPage__sgDK3']/ul//li[6]").click()
handle = brows.current_window_handle
brows.switch_to.window(handle)
#  点击下三页
time.sleep(3)
brows.find_element_by_xpath("//div[@class='ServiceResource_footerPage__sgDK3']/ul//li[8]").click()
handle = brows.current_window_handle
brows.switch_to.window(handle)
time.sleep(3)
# 找到第六页
brows.find_element_by_xpath("//div[@class='ServiceResource_footerPage__sgDK3']/ul//li[5]").click()
handle = brows.current_window_handle
brows.switch_to.window(handle)
time.sleep(3)
# # 第六页下面的div
# data_page = brows.find_elements_by_xpath('//div[@class="ServiceResource_sourcesList__2Rwcf"]/ul//li')
# for da in data_page:
#     print(da.text)
#     with open('tianditu-w.txt', 'a', encoding='utf-8') as f:
#         f.write(da.text + "\n")
for i in range(85):
    time.sleep(3)
    brows.find_element_by_xpath("//div[@class='ServiceResource_footerPage__sgDK3']/ul/li[@class='ant-pagination-next']").click()
    handle = brows.current_window_handle
    brows.switch_to.window(handle)
    time.sleep(3)
    data_page = brows.find_elements_by_xpath('//div[@class="ServiceResource_sourcesList__2Rwcf"]/ul//li')
    for da in data_page:
        print(da.text)
        with open('tianditu-w.txt', 'a', encoding='utf-8') as f:
            f.write(da.text + "\n")
# if i == 5:
#     time.sleep(3)
#     brows.find_element_by_xpath("//div[@class='ServiceResource_footerPage__sgDK3']/ul/li[@class='ant-pagination-next']").click()
#     handle = brows.current_window_handle
#     brows.switch_to.window(handle)
#     time.sleep(3)
#     data_page = brows.find_elements_by_xpath('//div[@class="ServiceResource_sourcesList__2Rwcf"]/ul//li')
#     for da in data_page:
#         print(da.text)
#         with open('tianditu-w.txt', 'a', encoding='utf-8') as f:
#             f.write(da.text + "\n")


# for i in range(3):
#     time.sleep(3)
#     brows.find_element_by_xpath("//div[@class='ServiceResource_footerPage__sgDK3']/ul/li[@class='ant-pagination-next']").click()
#     handle = brows.current_window_handle
#     brows.switch_to.window(handle)
#     time.sleep(3)
#     data_page = brows.find_elements_by_xpath('//div[@class="ServiceResource_sourcesList__2Rwcf"]/ul//li')
#     for da in data_page:
#         print(da.text)
#         with open('tianditu-w.txt', 'a', encoding='utf-8') as f:
#             f.write(da.text + "\n")
